﻿using Lbb.User;
using Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Lbb.Email.Web.Areas.admin.Controllers
{
    public class EmailHelpController : Controller
    {
        //
        // GET: /admin/EmailHelp/

        public ActionResult Reg()
        {
            return View();
        }
        public ActionResult AapprovedUser(string userID)
        {
            ViewBag.IsSuccess = false;
            try
            {
                MemberUser userName = MemberUser.GetUser(new Guid(userID));
                userName.IsApproved = true;
                userName.Save();
                ViewBag.IsSuccess = true;
            }
            catch (Exception e)
            {
                ViewBag.Msg = e.Message;
            }
            return View();
        }
        [HttpPost]
        public ActionResult Reg(string userName, string email)
        {
            try
            {
                ViewBag.IsSuccess = false;
                var wsatDefaultRole = "Member"; // default Role for membership
                var memberUser = MemberUser.GetUser(userName);
                if (memberUser == null)
                {
                     var password = Membership.GeneratePassword(Membership.MinRequiredPasswordLength, Membership.MinRequiredNonAlphanumericCharacters);
                    var user=MemberUser.Register(userName, email, password,throwError:true);
                     if (user != null)
                     {
                         user.IsApproved = false;
                         user.Save();
                     }
                     else
                     {
                         throw new Exception(AccountRes.RegErrorMsg);
                     }
                    if (Roles.RoleExists(wsatDefaultRole))
                    {
                        // Add the newly created user to the default Role.
                        if (!Roles.IsUserInRole(userName, wsatDefaultRole))
                        {
                            Roles.AddUserToRole(userName, wsatDefaultRole);
                        }
                        GenerateEmail(userName, email,password, (Guid)user.ProviderUserKey);
                        ViewBag.IsSuccess = true;
                    }
                    else
                    {
                        throw new Exception(AccountRes.SystemErrorMessage);
                    }
                }
                else
                {
                    throw new Exception(AccountRes.RegMsg);
                }
            }
            catch (Exception e)
            {
                ViewBag.Msg = e.Message;
            }
            return View();
        }
        public ActionResult ForgotPassword()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ForgotPassword(string userName)
        {
            ViewBag.IsSuccess = false;
            try
            {
                var user = MemberUser.GetUser(userName);
                if (user != null)
                {
                    var password = user.GetPassword();
                    
                    var body = "您的密码是：" + password + "请妥善保管！";
                    SendEmail(user.UserName, user.Email, body, "找回密码");
                    ViewBag.IsSuccess = true;
                }
                else
                {
                    throw new Exception("用户名不存在！");
                }
            }
            catch(Exception e)
            {
                ViewBag.Msg = e.Message;
            }
            return View();
        }
        public void GenerateEmail(string userName, string email,string password,Guid userID)
        {
            var body = "<a href=" +WebConfig.ApprovedUrl+"?userID=" + userID + ">点此激活<a>以激活您的账号，您的密码：" + password + "请妥善保存以便更改！";
            System.Net.Mail.MailMessage MyMailer = new System.Net.Mail.MailMessage();
            MyMailer.Bcc.Add(email);
            MyMailer.Subject = "激活您的账号";
            MyMailer.Body = "亲爱的 "+userName + ", <br/><br/>" + body;
            MyMailer.IsBodyHtml = Convert.ToBoolean(true);
            MyMailer.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            client.Send(MyMailer);
        }
        public void SendEmail(string userName, string email,string body,string title)
        {
            System.Net.Mail.MailMessage MyMailer = new System.Net.Mail.MailMessage();
            MyMailer.Bcc.Add(email);
            MyMailer.Subject = title;
            MyMailer.Body = "亲爱的 " + userName + ", <br/><br/>" + body;
            MyMailer.IsBodyHtml = Convert.ToBoolean(true);
            MyMailer.Priority = MailPriority.High;
            SmtpClient client = new SmtpClient();
            client.Send(MyMailer);
        }
    }
}
